/*
 *
 *  * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 *
 *
 */

package com.iwindplus.base.redis;

import com.iwindplus.base.redis.aspect.RedisLockAspect;
import com.iwindplus.base.redis.domain.property.RedisProperty;
import com.iwindplus.base.redis.strategy.CustomLockFailureStrategy;
import com.iwindplus.base.redis.strategy.CustomLockKeyBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Redis分布式锁配置.
 *
 * @author zengdegui
 * @since 2018/9/5
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({RedisProperty.class})
@ConditionalOnProperty(prefix = "spring.redis.lock", name = "enabled", havingValue = "true", matchIfMissing = true)
public class RedisLockConfiguration {

    /**
     * 创建 RedisLockAspect.
     *
     * @return RedisLockAspect
     */
    @Bean
    public RedisLockAspect redisLockAspect() {
        RedisLockAspect redisLockAspect = new RedisLockAspect();
        log.info("RedisLockAspect={}", redisLockAspect);
        return redisLockAspect;
    }


    /**
     * 创建 CustomLockFailureStrategy.
     *
     * @return CustomLockFailureStrategy
     */
    @Bean
    public CustomLockFailureStrategy customLockFailureStrategy() {
        CustomLockFailureStrategy customLockFailureStrategy = new CustomLockFailureStrategy();
        log.info("CustomLockFailureStrategy={}", customLockFailureStrategy);
        return customLockFailureStrategy;
    }

    /**
     * 创建 CustomLockKeyBuilder.
     *
     * @return CustomLockKeyBuilder
     */
    @Bean
    public CustomLockKeyBuilder customLockKeyBuilder() {
        CustomLockKeyBuilder customLockKeyBuilder = new CustomLockKeyBuilder();
        log.info("CustomLockKeyBuilder={}", customLockKeyBuilder);
        return customLockKeyBuilder;
    }
}
